<template>
    <div>
        <nav-bar>
            <!-- 有具名 -->
            <!-- <template v-slot:left>&lt;</template>   -->
            <template v-slot:default>用户登录</template>
            <!--默认-->
        </nav-bar>

        <div style="margin-top: 50px">
            <!-- logo -->
            <div style="text-align:center">
                <van-image
                    width="10rem"
                    height="10rem"
                    fit="contain"
                    src="https://img-blog.csdnimg.cn/20191122101006633.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtb25rZXlfY29t,size_16,color_FFFFFF,t_70"
                />
            </div>
            <van-form @submit="onSubmit">
            <van-field
                    v-model="email"
                    name="用户名"
                    label="用户名"
                    placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]"
                />
                <van-field
                    v-model="password"
                    type="password"
                    name="密码"
                    label="密码"
                    placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
                />
                <div style="margin: 16px;">
                    <div class="link-login" @click="$router.push({path: '/register'})">
                        没有账号，立即注册
                    </div>
                    <van-button round block type="info" color="#44b883" native-type="submit">提交</van-button>
                </div>
            </van-form>
        </div>

        
    </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import {login} from "network/user";
import {ref, reactive, toRefs} from 'vue';
import { Notify, Toast  } from 'vant';
import {useRouter} from 'vue-router'
import {useStore} from "vuex";
    export default {
        name: "Login",
        components: {
            NavBar
        },
        setup() {
            const router = useRouter();
            const store = useStore();

            const userinfo = reactive({
                email: '',
                password: '',
            })

            const onSubmit = () => {
                login(userinfo).then(res=> {
                    console.log(res.access_token);
                    //将token保存到本地 window.localStorage   setItem(key, value) getItem(key)
                    window.localStorage.setItem('token', res.access_token)
                    // 在vuex isLogin
                    store.commit('setIsLogin', true);

                    Toast.success('登录成功');
                    userinfo.email = '';
                    userinfo.password = '';
                    setTimeout(() => {
                        router.go(-1);
                    }, 500)
                })
                
            }

            return {
                ...toRefs(userinfo),
                onSubmit
            }
        }
    }
</script>

<style scoped>
    .link-login {
        /* float: left; */
        font-size: 14px;
        margin-bottom: 20px;
        color: #42b983;
        display: inline-block;
        text-align: left;
    }
</style>